package top.xin1901.j2ecd.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import top.xin1901.j2ecd.beans.Group;
import top.xin1901.j2ecd.common.response.R;
import top.xin1901.j2ecd.service.GroupService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author HeYunjia
 */

@RestController
@CrossOrigin("*")
@RequestMapping("/group")
public class GroupController {

    @Resource
    GroupService groupService;

    @PostMapping
    public R postGroupOne(@RequestBody Group group) {
        return R.success()
                .add("group", groupService.insert(group));
    }



    @GetMapping
    public R getGroupALl() {
        return R.success()
                .add("groups", groupService.selectAll());
    }

    @GetMapping("/{pageNumber}/{pageSize}")
    public R getGroupPage(@PathVariable Integer pageNumber,
                         @PathVariable Integer pageSize) {

        PageHelper.startPage(pageNumber, pageSize);

        List<Group> lists = groupService.selectAll();

        return R.success()
                .add("page", new PageInfo<>(lists));
    }

    @GetMapping("/{groupId}")
    public R getGroupOne(@PathVariable Long groupId) {
        return R.success()
                .add("group", groupService.selectOne(groupId));
    }


}
